package com.meta.platform.login.plus.wechat.rest;

import javax.annotation.Resource;

import com.meta.framework.core.ResponseVo;
import com.meta.platform.login.plus.common.vo.LoginSuccessVo;
import com.meta.platform.login.plus.wechat.proxy.MutipWeixinService;
import com.meta.platform.login.plus.wechat.rest.req.GenerateImgCmd;
import com.meta.platform.login.plus.wechat.rest.req.WeichatAuthCmd;
import com.meta.platform.login.plus.wechat.rest.req.WeichatCodeLoginCmd;
import com.meta.platform.login.plus.wechat.rest.res.WechatAuthVo;
import com.meta.platform.login.plus.wechat.rest.res.WechatUserInfoVo;
import org.springframework.web.bind.annotation.*;

import com.meta.framework.common.core.controller.BaseController;
import com.meta.framework.core.CommonRes;
import com.meta.platform.login.plus.wechat.http.resp.JsCode2SessionResponse;
import com.meta.platform.login.plus.wechat.rest.req.WeChatMinDecryptPhoneNumberCmd;
import com.meta.platform.login.plus.wechat.service.WeChatMinProgramService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 微信小程序 Rest API
 *
 * @author pengjianfeng
 * @version 1.0
 * @date 2021/1/21 5:13 下午
 */
@Api
@RestController
@RequestMapping("weChat/min")
public class WeChatMinProgramController extends BaseController {

    @Resource
    private WeChatMinProgramService weChatMinProgramService;

    @Resource
    private MutipWeixinService mutipWeixinService;

    /**
     * 接收微信小程序code，通过微信接口服务生成session_key
     *
     * @param cmd
     */
    @PostMapping("/auth")
    @ApiOperation(value = "发送微信小程序code", notes = "发送微信小程序code", tags = {"小程序"})
    public ResponseVo<WechatAuthVo> code(@RequestBody WeichatAuthCmd cmd) {
        JsCode2SessionResponse res = this.weChatMinProgramService.getSessionKey(cmd);
        return null != res && res.isSuccess() ?
                ResponseVo.success(WechatAuthVo.builder().openId(res.getOpenId()).unionId(res.getUnionId()).build())
            : ResponseVo.error(res.getErrCode(), res.getErrMsg());
    }

    /**
     * 微信小程序解密手机号码，并注册登录
     *
     * @param cmd
     */
    @PostMapping("/decrypt")
    @ApiOperation(value = "解密手机号码并注册登录", notes = "解密手机号码并注册登录", tags = {"小程序"})
    public ResponseVo<LoginSuccessVo> decrypt(@RequestBody WeChatMinDecryptPhoneNumberCmd cmd) {
        return ResponseVo.success(this.weChatMinProgramService.registerAndLogin(cmd));
    }

    /**
     * 微信小程序code，并注册登录
     *
     * @param cmd
     */
    @PostMapping("/code/login")
    @ApiOperation(value = "code码登录", notes = "code码登录", tags = {"小程序"})
    public ResponseVo<LoginSuccessVo> decrypt(@RequestBody WeichatCodeLoginCmd cmd) {
        return ResponseVo.success(this.weChatMinProgramService.codeLogin(cmd));
    }

    /**
     * 获取微信用户信息
     *
     * @param decryptPhoneNumberCmd
     */
    @PostMapping("/getUserInfo")
    @ApiOperation(value = "获取微信用户信息", notes = "获取微信用户信息", tags = {"小程序"})
    public ResponseVo<WechatUserInfoVo> getUserInfo(@RequestBody WeChatMinDecryptPhoneNumberCmd decryptPhoneNumberCmd) {
        return ResponseVo.success(this.weChatMinProgramService.getUserInfo(decryptPhoneNumberCmd));
    }

    @GetMapping("/generateImg")
    @ApiOperation(value = "生成微信图片", notes = "生成微信图片", tags = {"小程序"})
    public ResponseVo<String> getUserInfo(GenerateImgCmd cmd) {
        return ResponseVo.success("操作成功", this.mutipWeixinService.generateWechatImage(cmd.getAppId(), cmd.getPage(), cmd.getSence()));
    }
}
